//
//  LevelUpView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/2/10.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "LevelUpView.h"

@interface LevelUpView () <UITextFieldDelegate>

@property (nonatomic, strong) UIView *textBgView;
@property (nonatomic, copy) UILabel *titleLabel;
@property (nonatomic, copy) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *levelUpBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *inviteLabel;
@property (nonatomic, strong) UITextField *inviteTextField;

@end

@implementation LevelUpView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSelf];
        [self createSubviews];
    }
    return self;
}


#pragma mark - 会员升级
- (void)levelUpBtnClick:(UIButton *)sender {
    if (self.levelUpBlock) {
        self.levelUpBlock(self.inviteTextField.text);
    }
}


- (void)configSelf {
    
    self.backgroundColor = kBaseGrayColor;
    
}

#pragma mark - 
- (void)textFieldDidChanged:(UITextField *)textField {
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == self.inviteTextField) {
        return (text.length > 11 || ![string isOnlyNumber]) ? NO : YES;
    }
    return YES;
}

- (void)createSubviews {
    
    _textBgView = [UIView new];
    _textBgView.backgroundColor = WhiteColor;
    
    
    _titleLabel = [UILabel getLabWithText:@"" textFont:Font_17 textColor:BlackColor textAlignment:NSTextAlignmentLeft numberOfLines:1];
    _contentLabel = [UILabel getLabWithText:@"" textFont:Font_15 textColor:BlackColor textAlignment:NSTextAlignmentLeft numberOfLines:0];
    
    _lineView = [UIView new];
    _lineView.backgroundColor = kBaseGrayColor;
    
    _inviteLabel = [UILabel getLabWithText:@"邀请人:" textFont:Font_15 textColor:BlackColor];
    
    _inviteTextField = [[UITextField alloc] init];
    _inviteTextField.font = Font(15);
    _inviteTextField.delegate = self;
    _inviteTextField.keyboardType = UIKeyboardTypeNumberPad;
    _inviteTextField.borderStyle = UITextBorderStyleNone;
    _inviteTextField.placeholder = @"请输入邀请人手机号(必填项)";
    [_inviteTextField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    
    _levelUpBtn = [UIButton getClickBtnWithTitle:@"确认升级"];
    _levelUpBtn.enabled = YES;
    [_levelUpBtn addTarget:self action:@selector(levelUpBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                   
    [self addSubview:self.textBgView];
    [self addSubview:_titleLabel];
    [self addSubview:_contentLabel];
    [self addSubview:_lineView];
    [self addSubview:_inviteLabel];
    [self addSubview:_inviteTextField];
    [self addSubview:self.levelUpBtn];
    
    [self addConstraints];
}

- (void)addConstraints {
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*Scale_W);
        make.right.mas_equalTo(-15*Scale_W);
        make.top.mas_equalTo(25*Scale_H);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(self).offset(-15*Scale_W);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10*Scale_H);
    }];


    if ([[UserDefaults objectWithKey:@"pid"] isEqualToString:@"0"]) {
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(15*Scale_H);
            make.height.mas_equalTo(10*Scale_H);
        }];
        
        [self.inviteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.width.mas_equalTo(60*Scale_W);
            make.height.mas_equalTo(35*Scale_H);
            make.top.mas_equalTo(self.lineView.mas_bottom).offset(10*Scale_H);
        }];
        
        [self.inviteTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.inviteLabel.mas_right).offset(0*Scale_W);
            make.centerY.height.mas_equalTo(self.inviteLabel);
            make.right.mas_equalTo(self.titleLabel);
        }];
        
        [self.textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(self.titleLabel).offset(-15*Scale_H);
            make.bottom.mas_equalTo(self.inviteTextField).offset(10*Scale_H);
        }];
        
    } else {
        [self.textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(self.titleLabel).offset(-15*Scale_H);
            make.bottom.mas_equalTo(self.contentLabel).offset(15*Scale_H);
        }];
    }
    
    [self.levelUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textBgView.mas_bottom).offset(20*Scale_H);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
        make.height.mas_equalTo(44*Scale_H);
    }];
}

- (void)setTitleString:(NSString *)titleString {
    _titleString = titleString;
    _titleLabel.text = titleString;
}



- (void)setContentString:(NSString *)contentString {
    _contentString = contentString;
    
    // 富文本
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8*Scale_H;//行间距
    paragraphStyle.firstLineHeadIndent = 30*Scale_W;//首行缩进
    paragraphStyle.alignment = NSTextAlignmentJustified;// 两端对齐
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……省略
    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向（一共三种）
    
    NSDictionary *attributes = @{
//                                 NSFontAttributeName:[UIFont systemFontOfSize:13*Scale_W],// 字号
                                 NSParagraphStyleAttributeName:paragraphStyle,// 段落样式
                                 NSForegroundColorAttributeName:BlackColor,// 字体颜色
                                 };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentString attributes:attributes];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:RedColor range:[contentString rangeOfString:@"首页"]];
    [attributedString addAttribute:NSForegroundColorAttributeName value:RedColor range:[contentString rangeOfString:@"充值"]];
    [attributedString addAttribute:NSForegroundColorAttributeName value:RedColor range:[contentString rangeOfString:@"交易"]];
    [attributedString addAttribute:NSForegroundColorAttributeName value:RedColor range:[contentString rangeOfString:@"购买"]];
    _contentLabel.attributedText = attributedString;
}

- (void)setAvailable:(BOOL)available {
    _available = available;
    if (available) {
        _levelUpBtn.alpha = 1;
    } else {
        _levelUpBtn.alpha = 0;
    }
}

@end
